package com.hnkywl.sts.service.organ;

import com.hnkywl.sts.dao.organ.LxrDao;
import com.hnkywl.sts.entity.base.Lxr;
import com.hnkywl.sts.util.BaseUtils;
import net.ruixin.orm.hibernate.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * User: Administrator
 * Date: 13-3-25
 * Time: 下午3:38
 * To change this template use File | Settings | File Templates.
 */
@Service
public class LxrService {
     private LxrDao lxrDao;


    /**
     * 查询联系人
     *
     * @param id
     * @return
     */
    @Transactional
    public Lxr findLxrById(Long id){
        return this.lxrDao.get(id);
    }
    /**
     * 查询联系人列表
     *
     * @param xm       联系人名称
     * @param pageNo
     * @param pageSize
     * @return
     */
    @Transactional
    public String findXm(String xm,int pageNo, int pageSize){
        Page<Lxr> LxrPage = this.lxrDao.findLxrList(xm, pageNo, pageSize);
        List<Lxr> LxrList = LxrPage.getResult() == null?new ArrayList<Lxr>():LxrPage.getResult();
        String LxrJson = BaseUtils.fetchCollectionToJson(LxrList,"id","xm","sjhm","bz");
        return "[" + LxrJson + "]";
    }

    /**
     * 保存联系人信息
     *
     * @param lxr 联系人实体
     * @return
     */
    @Transactional
    public void saveLxr(Lxr lxr) {
        lxr.setDateCreated(new Date());
        lxr.setLastUpdated(lxr.getDateCreated());
        this.lxrDao.save(lxr);
    }


    /**
     * ajax远程验证
     * @param value 验证值
     * @param field 验证字段
     * @return
     */
    public boolean ajaxValidate(Long id,String value, String field) {
        return lxrDao.checkUnique(id,value,field);
    }
    //==================get and set ==============

    @Autowired
    public void setLxrDao(LxrDao lxrDao) {
        this.lxrDao = lxrDao;
    }
}
